// pages/farm/search/search.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keywrod: '',
    searchStatus: false,
    keywordvalue:false,
    helpKeyword:[],
    types:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 取消搜索
   */
  closeSearch: function () {
    wx.navigateBack()
  },
  /**
   * 从input栏里获取value值
   */
  inputChange: function (e) {
    this.setData({
      keyword: e.detail.value,
      searchStatus: false
    });
    this.getHelpKeyword();
  },
  /**
   * 当搜索到关键字后，将关键字输入到输入框
   */
  onKeywordTap: function (event) {
    this.getSearchResult(event.target.dataset.keyword);
  },
  getSearchResult(keyword) {
    console.log("设置关键字"+keyword.name);
    this.setData({
      keyword: keyword.name,
      keywordvalue: false
    });
    this.getQuery(keyword.name);

  },
  /**
   * 根据关键字搜索
   */
  getQuery:function(keyword){
    let that = this;
    util.request(api.FarmInfoSearch, { keyword:keyword }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          types: res.data,
          searchStatus: true
        });
      }
    });
  },
  /**
   * 搜索帮助
   */
  getHelpKeyword: function () {
    let that = this;
    util.request(api.FarmInfoSearch, { keyword: that.data.keyword }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          helpKeyword: res.data,
          keywordvalue: true
        });
      }
    });
  },
  /**
   * 输入框获取焦点
   */
  inputFocus: function () {
    this.setData({
      searchStatus: false,
      
    });
    if (this.data.keyword) {
      this.getHelpKeyword();
    }
  },
  /**
   * 通过关键词展示
   */
  getGoodsList: function () {
    let that = this;
    util.request(api.GoodsList, { keyword: that.data.keyword, page: that.data.page, size: that.data.size, sort: that.data.currentSortType, order: that.data.currentSortOrder, categoryId: that.data.categoryId }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          searchStatus: true,
          categoryFilter: false,
          goodsList: res.data.goodsList,
          filterCategory: res.data.filterCategory,
          page: res.data.currentPage,
          size: res.data.numsPerPage
        });
      }

      //重新获取关键词
    });
  }
})